<?php
/**
 * Created by PhpStorm.
 * User: ysfl
 * Date: 16-1-25
 * Time: 下午11:41
 */

namespace modules\gwbn;

use Cross\MVC\Module;

class PanelModule extends Module
{

    //上传头像
    function User_Avatars($data)
    {

        //上传文件限制条件

            //限制大小
            $size = 2097152;

            //限制格式
            $type = array(
                'image/gif', 'image/jpg', 'image/png', 'image/jpeg' , 'image/webp'
            );

        //上传文件检测


        //检测文件后缀
        if (!in_array($data['type'], $type)) {
            return "暂时不支持这个".$data['type']."格式";
        }

        //检测文件大小
        if ($data['size'] > $size) {
            return "文件大小不能超过".$size."格式";
        }

        //获得图片后缀
        $ext_name = strtolower(end(explode('.',$data['name'])));

        //给图片设置名字，用户id.图片后缀
        $img_name = $_SESSION['uid'].".".$ext_name;

        //储存路径
        $img_dir = "avatars/".$img_name;

        //存到服务器本地文件夹里面
        move_uploaded_file($data['tmp_name'],$img_dir);

        $this->link->update('gwbn_admin',array('avatars' => $ext_name),array('id' => $_SESSION['uid']));

        $ext = $this->link->get('admin','avatars','id ='. $_SESSION['uid']);

        $_SESSION['avatars'] = $ext['avatars'];

        return "上传成功";
    }

    //登录次数
    function User_Login_Num()
    {
        $n = $this->link->select('count(*) as num')
            ->from('gwbn_admin_login_log')
            ->where(array('aid' => $_SESSION['uid']))
            ->stmt()
            ->fetchAll(\PDO::FETCH_ASSOC);

        return $n[0]['num'];
    }
}